package main

import (
	"context"
	"fmt"
	"sync"
)

type Manager struct {
	size    uint
	dealers []DealerInf

	ctx    context.Context
	cancle context.CancelFunc
	group  *sync.WaitGroup
}

type DealerInf interface {
	Run(ctx context.Context)
}

func (m *Manager) InitManager(size uint, ctx context.Context, group *sync.WaitGroup, kind interface{}) {
	for i := uint(0); i < size; i++ {
		dealer := NewDealer(kind, group)
		m.dealers = append(m.dealers, dealer)
	}
	m.size = size
	m.ctx, m.cancle = context.WithCancel(ctx)
	m.group = group
	fmt.Println("Manager initialized success,the size of dealers is, ", size)
}

func NewDealer(kind interface{}, group *sync.WaitGroup) DealerInf {
	switch kind.(type) {
	case UrlDealer:
		return NewUrlDealer(group)
	case PageDealer:
		return NewPageDealer(group)
	case ImageDealer:
		return NewImageDealer(group)
	default:
		fmt.Println("Wrong Type")
		return nil
	}
}

func (m *Manager) Run() {
	for _, dealer := range m.dealers {
		go dealer.Run(m.ctx)
	}
}

func (m *Manager) Stop() {
	m.cancle()
}
